//
//  GoodsListVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/9.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "GoodsListVC.h"
#import "GoodListModel.h"
#import "GoodListCell.h"
#import "GoodListSectionHeadView.h"

@interface GoodsListVC () <UITableViewDelegate,UITableViewDataSource>{
    int page;
}

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation GoodsListVC

-(NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [NSArray array];
    }
    return _titleArray;
}

-(NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight)];
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, self.scrollView.nim_height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = HexColor(0xffffff);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForData];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForData];
        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"货物清单";
    page = 1;
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:ReceiptDeliveryGoodList params:@{@"dp_id":self.dp_id,@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.titleArray = [responseObject objectForKey:@"header"];
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [NSMutableArray arrayWithArray:responseObject[@"list"]];
            }else {
                NSArray *array = responseObject[@"list"];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self configFrame];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)configFrame {
    NSArray *dic = self.array.firstObject;
    self.scrollView.contentSize = CGSizeMake(GoodList_ViewWidth * dic.count, self.scrollView.bounds.size.height);
    self.tableView.nim_width = self.scrollView.contentSize.width;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    GoodListSectionHeadView *view = [[GoodListSectionHeadView alloc] initWithFrame:CGRectMake(0, 0, GoodList_ViewWidth * self.titleArray.count, 45)];
    view.titleArray = self.titleArray;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodListCell *cell = [GoodListCell GoodListCellWithTableView:tableView];
    cell.dic = self.array[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodListCell *cell = [GoodListCell GoodListCellWithTableView:tableView];
    cell.dic = self.array[indexPath.row];
    return cell.height;
}

@end
